package vma.nicici.chat.ui;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import vma.nicici.chat.R;
import vma.nicici.chat.api.photos.PhotosDb;
import vma.nicici.chat.communication.AsynchCallback;
import vma.nicici.chat.gson.Message;
import vma.nicici.chat.history.HistoryDatabase;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.provider.CalendarContract.CalendarAlerts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MessageArrayAdapter extends ArrayAdapter<Message> {

	private static final String DATE_FORMAT = "yyyy-MM-dd'T'hh:mm:ssZZZZZ";
	
	private final Activity activity;
	private ArrayList<Message> messages;
	private int count;
	
	HistoryDatabase history;

	

	public MessageArrayAdapter(Activity context, ArrayList<Message> objects,HistoryDatabase history) {
		super(context, R.layout.messages_rowlayout, objects);		
		messages = objects;
		this.activity = context;
		this.history=history;
		count = messages.size();
		
	}
	
	@Override
	public int getCount() {	
		return count;
	}
	
	@Override
	public Message getItem(int position) {
		return messages.get(position);
	}
	
	public void setMessages(ArrayList<Message> objects){
		this.messages = objects;
		count = messages.size();
	}

	private String parseDate(String timestamp) {
		SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
		Calendar date = Calendar.getInstance();
		try {
			date.setTime(formatter.parse(timestamp));
		} catch (ParseException e) {
			return "bad timestamp!";
		}		
		String minute = "" + date.get(Calendar.MINUTE);
		if (minute.length() == 1 ) minute = "0"+minute;
		String parsedDate = date.get(Calendar.DAY_OF_MONTH) + ". " 
		+ new DateFormatSymbols().getMonths()[date.get(Calendar.MONTH)] + " " + date.get(Calendar.HOUR_OF_DAY) + ":" + minute ;
		return parsedDate;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 View rowView = convertView;
		if (rowView == null) {
		  LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	      rowView = inflater.inflate(R.layout.messages_rowlayout, parent,false);
		}
		
		Message msg=messages.get(position);
		
		if (msg != null) {
			final TextView timestamp = (TextView) rowView.findViewById(R.id.twTimestamp);
			final TextView username = (TextView) rowView.findViewById(R.id.twUsername);
			final TextView messageArea = (TextView) rowView.findViewById(R.id.twMessageArea);
			final ImageView photo = (ImageView) rowView.findViewById(R.id.iwFbPhoto);		
			timestamp.setText(parseDate(msg.timestamp));	
		
			username.setText(activity.getString(R.string.working));
			
			history.getNameAsynch(msg.facebookUserID, new AsynchCallback<String>() {
				@Override
				public void onFinished(final String response) throws IOException,
						URISyntaxException {
					activity.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							username.setText(response);
						}
					});
				}
			});			
			
			messageArea.setText(msg.messageText);
			
			
			if (msg.facebookUserID > 4) {
				PhotosDb.instance(activity).getPhotoFromMessageAsynch(msg, new AsynchCallback<Bitmap>() {
					@Override
					public void onFinished(final Bitmap response) throws IOException,
							URISyntaxException {
						  activity.runOnUiThread(new Runnable() {
							@Override
							public void run() {
									photo.setImageBitmap(response);
							}
						});
					}
				});
			} else {
				photo.setImageResource(R.drawable.fbdefault);
			}
			
			
		}
		
		return rowView;
	}
	


}
